/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import Utils.Utils;
import Utils.DBConnection;
import Utils.Settings;
import java.security.MessageDigest;
import java.sql.ResultSet;

/**
 *
 * @author user
 */
public class AdminModel {

    DBConnection db = new DBConnection(Settings.getDb(), Settings.getDbLogin(), Settings.getDbPassword());
    String message;

    public int ValidateUser(String username, String password) {

        try {
            if (username.isEmpty() || password.isEmpty()) {
                message = "Introduceti utilizator si parola!";
                return -1;
            }

            ResultSet rs = db.ExecuteQuery(
                    "SELECT * "
                    + "FROM admin "
                    + "WHERE Username =\"" + username + "\" AND Password =\"" + Utils.md5(password) + "\" ");

            if (!rs.next()) {
                message = "Username sau parola nu sunt corecte!";
                return -1;
            }

            return rs.getInt("AdminID");
        } catch (Exception ex) {
            return -1;
        } finally {
       //     db.close();
        }
    }

    public int ChangePassword(int adminID, String oldPassword, String newPassword) {
        try {
            if (oldPassword.isEmpty() || newPassword.isEmpty()) {
                message = "Completati toate campurile!";
                return -1;
            }

            int rows = db.ExecuteUpdate(
                    "UPDATE admin SET Password = \"" + Utils.md5(newPassword) + "\" "
                    + "WHERE AdminID=\"" + adminID + "\" AND Password=\"" + Utils.md5(oldPassword) + "\"");

            if (rows > 0) {
                message = "Parola a fost actualizata cu succes!";
                return 1;
            } else {
                message = "Parola nu a fost actualizata!";
                return -1;
            }

        } catch (Exception ex) {
            message = "Eroare cod 001";
            return -1;
        } finally {
       //     db.close();
        }
    }

    public String GetMessage() {
        return message;
    }
}
